package com.ruoyi.system.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.system.domain.DataMatching;
import com.ruoyi.system.mapper.DataMatchingMapper;
import com.ruoyi.system.service.IDataMatchingService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AllCustomsCodesMapper;
import com.ruoyi.system.domain.AllCustomsCodes;
import com.ruoyi.system.service.IAllCustomsCodesService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.UUID;

/**
 * 全部海关编码Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-07
 */
@Service
public class AllCustomsCodesServiceImpl implements IAllCustomsCodesService 
{
    @Autowired
    private AllCustomsCodesMapper allCustomsCodesMapper;
    @Autowired
    private DataMatchingMapper dataMatchingMapper;
    @Autowired
    private DataMatchingServiceImpl dataMatchingServiceImpl;
    @Autowired
    private SysDictTypeServiceImpl sysDictTypeServiceImpl;

    /**
     * 查询全部海关编码
     * 
     * @param id 全部海关编码主键
     * @return 全部海关编码
     */
    @Override
    public AllCustomsCodes selectAllCustomsCodesById(String id)
    {
        return allCustomsCodesMapper.selectAllCustomsCodesById(id);
    }

    /**
     * 查询全部海关编码列表
     * 
     * @param allCustomsCodes 全部海关编码
     * @return 全部海关编码
     */
    @Override
    public List<AllCustomsCodes> selectAllCustomsCodesList(AllCustomsCodes allCustomsCodes)
    {
        return allCustomsCodesMapper.selectAllCustomsCodesList(allCustomsCodes);
    }

    /**
     * 新增全部海关编码
     * 
     * @param allCustomsCodes 全部海关编码
     * @return 结果
     */
    @Override
    public int insertAllCustomsCodes(AllCustomsCodes allCustomsCodes)
    {
        return allCustomsCodesMapper.insertAllCustomsCodes(allCustomsCodes);
    }

    /**
     * 修改全部海关编码
     * 
     * @param allCustomsCodes 全部海关编码
     * @return 结果
     */
    @Override
    public int updateAllCustomsCodes(AllCustomsCodes allCustomsCodes)
    {
        return allCustomsCodesMapper.updateAllCustomsCodes(allCustomsCodes);
    }

    /**
     * 批量删除全部海关编码
     * 
     * @param ids 需要删除的全部海关编码主键
     * @return 结果
     */
    @Override
    public int deleteAllCustomsCodesByIds(String[] ids)
    {
        return allCustomsCodesMapper.deleteAllCustomsCodesByIds(ids);
    }

    /**
     * 删除全部海关编码信息
     * 
     * @param id 全部海关编码主键
     * @return 结果
     */
    @Override
    public int deleteAllCustomsCodesById(String id)
    {
        return allCustomsCodesMapper.deleteAllCustomsCodesById(id);
    }


    public void importExcelData(InputStream inputStream, String type) throws Exception {
//        Workbook workbook = new XSSFWorkbook(inputStream);
        Workbook workbook = new XSSFWorkbook(inputStream);
        SysDictType dictType = sysDictTypeServiceImpl.selectDictTypeById(11L);
        dictType.setStatus("0");
        sysDictTypeServiceImpl.updateDictType(dictType);
        Sheet sheet = workbook.getSheetAt(0);  // 获取第一个工作表
        // 清空表
        if (type != null && !type.isEmpty() && type.equals("1")){
            dataMatchingServiceImpl.deleteDataMatchingData();
        }
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            SysDictType sysDictType = sysDictTypeServiceImpl.selectDictTypeById(11L);
            if (sysDictType == null && sysDictType.getStatus().equals("1")) {
                dataMatchingServiceImpl.deleteDataMatchingData();
                break;
            }
            Row row = sheet.getRow(i);
            if (getCellValue(row, 0).isEmpty() && getCellValue(row, 1).isEmpty() && getCellValue(row, 2).isEmpty() && getCellValue(row, 3).isEmpty() && getCellValue(row, 4).isEmpty() && getCellValue(row, 5).isEmpty() && getCellValue(row, 6).isEmpty()) {
                continue;
            }
            DataMatching commodityInfo = new DataMatching();
            String id = UUID.randomUUID().toString().replace("-", "");
            commodityInfo.setId(id);
            commodityInfo.setEori(getCellValue(row, 0));
            commodityInfo.setClientsDescription(getCellValue(row, 1));
            commodityInfo.setClientsDescriptionInChinese(getCellValue(row, 2));
            commodityInfo.setMaterial(getCellValue(row, 3));
            commodityInfo.setClientsHsCode(getCellValue(row, 4));
            commodityInfo.setAsin(getCellValue(row, 5));
            commodityInfo.setCommodity(getCellValue(row, 6));
            commodityInfo.setHsCode(getCellValue(row, 7));
            commodityInfo.setThirdCountryDuty(getCellValue(row, 8));

            if (commodityInfo.getEori().isEmpty() && commodityInfo.getClientsDescription().isEmpty() && commodityInfo.getClientsDescriptionInChinese().isEmpty() && commodityInfo.getMaterial().isEmpty() && commodityInfo.getClientsHsCode().isEmpty() && commodityInfo.getAsin().isEmpty()) {
                continue;
            }
            if (type!=null && type.equals("1")){
                dataMatchingMapper.insertDataMatchingLs(commodityInfo);
            } else {
                dataMatchingMapper.insertDataMatching(commodityInfo);
            }
        }

//        Sheet sheet = workbook.getSheetAt(0);  // 获取第一个工作表
//        JSONObject res = new JSONObject();
//        int resNum=0;
//        int errorNum=0;
//        // 从第二行开始读取数据（第一行是表头）
//        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
//
//            Row row = sheet.getRow(i);
//            if (getCellValue(row, 0).isEmpty() && getCellValue(row, 1).isEmpty() && getCellValue(row, 2).isEmpty() && getCellValue(row, 3).isEmpty() && getCellValue(row, 4).isEmpty() && getCellValue(row, 5).isEmpty() && getCellValue(row, 6).isEmpty()) {
//                continue;
//            }
//            AllCustomsCodes commodityInfo = new AllCustomsCodes();
//            String id = UUID.randomUUID().toString().replace("-", "");
//            commodityInfo.setId(id);
//            commodityInfo.setCommodityCode(getCellValue(row, 0));
//            commodityInfo.setThirdCountryDuty(getCellValue(row, 1));
//            commodityInfo.setClassification(getCellValue(row, 2));
//            commodityInfo.setSection(getCellValue(row, 3));
//            commodityInfo.setSectionTitle(getCellValue(row, 4));
//            commodityInfo.setChapters(getCellValue(row, 5));
//            commodityInfo.setStatus(getCellValue(row, 6));
//            commodityInfo.setType(type);
//            commodityInfo.setStatus("1");
//            if (allCustomsCodesMapper.insertAllCustomsCodes(commodityInfo)>0){
//                resNum++;
//            }else{
//                errorNum++;
//            }
//
//
//        }
    }

    private String getCellValue(Row row, int colIndex) {
        Cell cell = row.getCell(colIndex);
        if (cell != null) {
            return cell.toString();
        }
        return "";
    }
}
